package com.iot08.api.utils.buckettool;

import com.iot08.common.utils.DateUtil;
import com.iot08.common.utils.RandomUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: Weichao
 * @Date: 2024/08/27/15:37
 * @Description:
 */
@Slf4j
@Component
public class BaseBucketTool {

    @Autowired
    private MiniIOUtilTool miniIOUtilTool;
    @Autowired
    private ALOSSUtil alossUtil;

    @Value("${bucket.type:0}")
    private Integer bucketType;

    public String getDOMAIN(){
        if(bucketType == 0){
            return ALOSSUtil.DOMAIN;
        } else if (bucketType == 3) {
            return MiniIOUtilTool.DOMAIN;
        }
        return "";
    }
    public static final String IMG_CONTENT_TYPE = "image/jpeg";

    public static final String IMG_PNG_CONTENT_TYPE = "image/png";
    public static final String AMR_CONTENT_TYPE = "audio/amr";
    public static final String ZIP_CONTENT_TYPE = "application/zip";

    public static final String DEVICE_COMMON_PATH = "static/devices/";
    public static final String CONTACT_COMMON_PATH = "static/contact/";
    public static final String ACCOUNT_COMMON_PATH = "static/account/";
    public static final String CHATGROUP_COMMON_PATH = "static/cgroups/";
    public static final String MESSAGE_COMMON_PATH = "static/message/";
    public static final String PHOTOGRAPH_COMMON_PATH = "static/photograph/";
    public static final String APPLOG_COMMON_PATH = "static/APPLog/";
    public static final String PPMESSAGE_COMMON_PATH = "static/ppmessage/";
    public static final String VOICEALARM_COMMON_PATH = "static/voicealarm/";
    public static final String VOICEALARM_COMPLAINT_PATH = "static/complaint/";

    /**
     * 微信小程序 生成的小程序码存储路径
     */
    public static final String WXSMALLPROGRAM_COMPLAINT_PATH = "static/wxSmallProgram/";

    // 媒体位图片
    public static final String ADVERT_COMPLAINT_PATH = "static/advert/";
    // 课程图片
    public static final String COURSE_COMPLAINT_PATH = "static/course/";

    //CMS 用户头像
    public static final String SYSUSER_COMPLAINT_PATH = "static/sysuser/";

    //mob 推送通知相关
    public static final String MOBNOTIFY_COMPLAINT_PATH = "static/mobNotify/";


    /**
     * 上传图片
     *
     * @param path
     * @param file
     * @time: 2020/10/22 17:59
     * @author: Rock
     * @version 1.0
     */
    public void upload(String path, MultipartFile file, int type) throws IOException {
        upload(path, file.getInputStream(), type);
    }

    public void upload(String path, InputStream inputStream, int type) throws IOException {
        if (bucketType == 0) {
            alossUtil.upload(path, inputStream, type);
        } else if (bucketType == 3) {
            miniIOUtilTool.upload(path, inputStream, type);
        }else{
            log.error("找不到对应的 存储桶操作方式");
            return;
        }
    }

    public boolean checkPath (String path){
        if (bucketType == 0) {
            return alossUtil.checkPath(path);
        } else if (bucketType == 3) {
            return miniIOUtilTool.checkPath(path);
        }else{
            log.error("找不到对应的 存储桶操作方式");
            return false;
        }
    }
    /**
     * @param filePath
     * @time: 2020/11/6 18:19
     * @author: Rock
     * @version 1.0
     */
    public Boolean delFile(String filePath) {
        if (bucketType == 0) {
            return alossUtil.delFile(filePath);
        } else if (bucketType == 3) {
            return miniIOUtilTool.delFile(filePath);
        } else {
            log.error("找不到对应的 存储桶操作方式");
            return false;
        }
    }

    /**
     * @param objectName
     * @time: 2021/3/22 16:50
     * @author: Rock
     * @version 1.0
     */
    public ByteArrayOutputStream downloadFileStream(String objectName) {
        if(bucketType == 0){
            return alossUtil.downloadFileStream(objectName);
        }else if (bucketType == 3){
            return miniIOUtilTool.downloadFileStream(objectName);
        }else{
            log.error("找不到对应的 存储桶操作方式");
            return null;
        }
    }

    public static String createFileName(MultipartFile file, String defFileType) {
        String fileType = defFileType;
        try {
            if (file.getOriginalFilename() != null &&  file.getOriginalFilename().contains("\\.")) {
                fileType = file.getOriginalFilename().split("\\.")[1];
            } else if (file.getContentType().contains("audio") ) {
                String[] split = file.getContentType().split("audio/");
                if(split.length > 1){
                    if(split[1].contains("wave")){
                        fileType = "wav";
                    } else if (split[1].contains("jpeg")) {
                        fileType = "jpg";
                    } else if (split[1].contains("mpeg")) {
                        fileType = "mp3";
                    } else{
                        fileType = split[1];
                    }
                }
            }else if (file.getContentType().contains("amr")) {
                fileType = "amr";
            }
        }catch (Exception e){
            log.info("取文件类型错误:"+e.getMessage()+ "file.getName():"+file.getName()+" file.getOriginalFilename():"+file.getOriginalFilename()+" getContentType:"+file.getContentType()
                    , e);
            fileType = defFileType;
        }
        return DateUtil.dateToStr(new Date(), DateUtil.FORMAT_5) + "APP" + RandomUtil.getRandomStr(13) + "."+fileType;
    }

    /**
     * @time: 2020/12/4 11:15
     * @author: Rock
     * @version 1.0
     */
    public static String createFileNameToJpg() {
        return DateUtil.dateToStr(new Date(), DateUtil.FORMAT_5) + "APP" + RandomUtil.getRandomStr(13) + ".jpg";
    }

    /**
     * @time: 2020/12/24 15:41
     * @author: Rock
     * @version 1.0
     */
    public static String createFileNameToAmr() {
        return DateUtil.dateToStr(new Date(), DateUtil.FORMAT_5) + "APP" + RandomUtil.getRandomStr(13) + ".amr";
    }

    /**
     * @time: 2021/1/14 17:22
     * @author: Rock
     * @version 1.0
     */
    public static String createFileNameToZip(String openid) {
        return DateUtil.dateToStr(new Date(), DateUtil.FORMAT_3) + openid + ".zip";
    }

}